<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{include file='comm/comm_cal.tpl'}-->
    <title><!--{if isset($card_apply_array)}-->修改申领单信息<!--{else}-->添加申领单信息<!--{/if}--></title>
</head>

<body>
<style>
    .imageSite{
        position: absolute;
        left:280px;
        top:8px;
    }
</style>
<div class="panel panel-default">


<div class="comm_right">

    <div class="comm_crub">
        <span class="now_crub">申领单信息</span>
    </div>

    <div class="comm_content">

        <div class="panel-body">

            <div style="position:relative">

                <form id="itemForm" onsubmit="return false;" class="form-horizontal">

                <div class="form-group" >
                      <label class="col-sm-2 control-label" for="get_card_type">
                          <span class="red">*</span>
                          领卡方式：
                      </label>
                    <div class="col-sm-10">
                        <select class="form-control w150" id="get_card_type" name="get_card_type">
                            <option value="1">个人</option>
                            <option value="2" selected>公司</option>
                        </select>
                        <input type="hidden" name="apply_id" id="apply_id" value="<!--{$card_apply_array.apply_id|default:0}-->">
                    </div>
                </div>

                <div class="form-group" >
                     <label class="col-sm-2 control-label" for="customer_name">
                         <span class="red">*</span>
                        客户名称：
                     </label>
                    <div class="col-sm-10">
                         <input type="text" class="form-control w150 fl" placeholder="请输入客户名称"  id="customer_name" name="customer_name" value="<!--{$card_apply_array.customer_name|default:''}-->" readonly="readonly" />
                         &nbsp;&nbsp;<button type="button" class="btn btn-primary" id="setMapBtn" onclick="cardApplyList.search(1);">查询客户</button>
                        <input type="hidden" value="<!--{$card_apply_array.company_id|default:0}-->" name="company_id" id="company_id"/>
                    </div>
                </div>

                <div class="form-group" >
                    <label class="col-sm-2 control-label" for="apply_date">
                        <span class="red">*</span>
                        申领日期：
                    </label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control w150" placeholder="点击选择申领日期"  id="apply_date" name="apply_date" readonly="readonly" value="<!--{$card_apply_array.apply_date|default:''}-->"/>
                    </div>
                </div>

                <div class="form-group" >
                    <label class="col-sm-2 control-label" for="linkman">
                        联系人：
                    </label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control w150" placeholder="请输入联系人"  id="linkman" name="linkman" value="<!--{$card_apply_array.linkman|default:''}-->" />
                    </div>
                </div>

                <div class="form-group" >
                    <label class="col-sm-2 control-label" for="contact_way">
                        联系方式：
                    </label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control w150" placeholder="请输入联系方式"  id="contact_way" name="contact_way" value="<!--{$card_apply_array.contact_way|default:''}-->" />
                    </div>
                </div>

                <div class="form-group" >
                    <label class="col-sm-2 control-label" for="save_remark">
                        备注：
                    </label>
                    <div class="col-sm-10">
                        <textarea id="remark" class="form-control w300" rows="3" name="remark"  placeholder="请输入备注"><!--{$card_apply_array.remark|default:''}--></textarea>
                    </div>
                </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label" for="save_remark">
                            回款方式：
                        </label>
                        <div class="col-sm-10">
                            <!--{foreach $backArray as $back}-->
                            <input  class="square-checkbox" type="checkbox" name="back[]" value="<!--{$back->itemID}-->" style=" width: 19px;height: 25px;padding: 0 5px 0 0;background: url(checkbox.png) no-repeat;"   <!--{if $back->itemID|in_array:$backArr}-->checked<!--{/if}-->><!--{$back->itemName}-->
                            <!--{/foreach}-->
                        </div>
                    </div>

                </form>
            </div>
        </div>
</div>

<div class="comm_right">

    <div class="comm_crub">
        <span class="now_crub">现金卡信息</span>
    </div>
    <div class="comm_content">
        <div class="panel-body">
            <div style="position:relative">

                <div id="intervalTime">

                    <form class="form-horizontal" id="addForm" name="addForm" onsubmit="return false;">

                        <div class="form-group" >
                             <label class="col-sm-2 control-label" for="face_value_id">
                                现金卡面值：
                             </label>
                            <div class="col-sm-10">
                                 <select class="form-control w150" id="face_value_id" name="face_value_id" onchange="updMon();">
                                     <option value="">请选择</option>
                                     <!--{foreach from=$fave_value_array item=fave_value}-->
                                     <option value="<!--{$fave_value.face_value_id}-->"><!--{$fave_value.face_value}-->—<!--{$fave_value.card_info}--></option>
                                     <!--{/foreach}-->
                                 </select>
                            </div>
                        </div>

                        <div class="form-group" >
                            <label class="col-sm-2 control-label" for="card_type_id">
                                代金卡类型：
                            </label>
                            <div class="col-sm-10">
                                <select class="form-control w150" id="card_type_id" name="card_type_id" >
                                    <option value="">请选择</option>
                                    <!--{foreach from=$cardType_array item=type}-->
                                    <option value="<!--{$type.card_type_id}-->"><!--{$type.card_type_name}--></option>
                                    <!--{/foreach}-->
                                </select>
                            </div>
                        </div>

                        <div class="form-group" >
                            <label class="col-sm-2 control-label" for="card_spec_id">
                                现金卡规格：
                            </label>
                            <div class="col-sm-10">
                                 <select class="form-control w150" id="card_spec_id" name="card_spec_id" >
                                     <option value="">请选择</option>
                                     <!--{foreach from=$card_spec_array item=card_spec}-->
                                     <option value="<!--{$card_spec.itemID}-->"><!--{$card_spec.itemName}--></option>
                                     <!--{/foreach}-->
                                 </select>
                            </div>
                        </div>

                        <div class="form-group" >
                            <label class="col-sm-2 control-label" for="card_num">
                                数量：
                            </label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control w150" placeholder="请输入数量"  id="card_num" name="card_num" onblur="updMon();" />
                            </div>
                        </div>

                        <div class="form-group" >
                            <label  class="col-sm-2 control-label" for="card_discount">
                                现金卡折扣：
                            </label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control w150" placeholder="请输入现金卡折扣"  id="card_discount" name="card_discount" />
                            </div>
                        </div>

                        <div class="form-group" >
                            <label class="col-sm-2 control-label" for="card_total_amount">
                                折后价：
                            </label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control w150" placeholder="请输入现金卡总金额"  id="card_total_amount" name="card_total_amount"/><span style="color: #808080" id="reference"></span>
                            </div>
                        </div>

                        <div class="form-group" >
                            <label  class="col-sm-2 control-label" for="remark">
                                备注：
                            </label>
                            <div class="col-sm-10">
                                <textarea class="form-control w300" rows="3" id="card_remark" name="card_remark" placeholder="请输入备注"></textarea>
                            </div>
                            <span style="padding: 0 245px;">
                                <button class="btn btn-default" onclick="cardApplyList.check();" type="button">新增</button>
                            </span>
                        </div>

                    </form>

                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr style="line-height: 45px;">
                            <th width="12%" style="text-align:center;">操作</th>
                            <th width="12%" style="text-align:center;">面额</th>
                            <th width="12%" style="text-align:center;">规格</th>
                            <th width="12%" style="text-align:center;">数量</th>
                            <th width="12%" style="text-align:center;">折扣率</th>
                            <th width="12%" style="text-align:center;">折后价</th>
                            <th width="12%" style="text-align:center;">类型</th>
                            <th width="12%" style="text-align:center;">备注</th>
                        </tr>
                        </thead>
                        <tbody id="add_tr">

                        <!--{if isset($card_detail_array)}-->
                            <!--{foreach from=$card_detail_array key=k item=detail}-->
                            <tr id="card_line_<!--{$k+1}-->">
                                <td class="tac"><a href="javascript:void(0);" onclick="cardApplyList.del(<!--{$k+1}-->);">删除</a></td>
                                <td class="tac"><!--{$detail.face_value}--></td>
                                <td class="tac"><!--{$detail.card_spec}--></td>
                                <td class="tac"><!--{$detail.card_num}--></td>
                                <td class="tac"><!--{$detail.card_discount}--></td>
                                <td class="tac"><!--{$detail.card_total_amount}--></td>
                                <td class="tac"><!--{$detail.card_type}--></td>
                                <td class="tac"><!--{$detail.remark}--></td>
                            </tr>
                            <!--{/foreach}-->
                        <!--{/if}-->

                        </tbody>
                    </table>
                </div>

            </div>
        </div>
    </div>
</div>


<div class="bottom_fixbox">
    <div class="tac" style="background: #dddddd;">
        <input type="button" class="btn btn-primary" onclick="cardApplyList.save();" value="保存" />&nbsp;&nbsp;&nbsp;
        <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
    </div>
</div>
</div>

<script type="text/javascript">
function updMon(){
    $("#card_total_amount").val('');//折后价清空
    var face_value = $('#face_value_id').find('option:selected').text();
    var strs = new Array();
    strs = face_value.split("—");
    face_value=strs[0];
    var card_num = $('#card_num').val();
    var card_discount = $('#card_discount').val();
    var error_msg = '';
    var item_id = '';

    if (face_value != '请选择' && card_num!='' && E.isDigital(card_num) && card_discount!='' &&( card_discount > 0 || card_discount < 1) ) {
        if (error_msg != '') {
            E.alert(error_msg, 1, function() {
                $('#' + item_id).focus();
            });
        } else {
            var total_amount = face_value * card_num * card_discount;
            //将参考金额写进输入框里，并修改输入框的属性 add by loreta.cheng 2015-06-15 start
            $("#card_total_amount").val(Math.round(total_amount));
            document.getElementById("card_total_amount").disabled = true;
            //将参考金额写进输入框里，并修改输入框的属性 add by loreta.cheng 2015-06-15 end
            //$('#reference').html("参考金额："+parseFloat(total_amount).toFixed(2));
        }
    }
}

$(window).ready(function() {

    $('#get_card_type').change(function() {

        if ($(this).val() == 1) {
            $('#setMapBtn').hide();
            $('#customer_name').attr('readonly', false);
        } else {
            $('#setMapBtn').show();
            $('#customer_name').attr('readonly', true);
        }
        $('#customer_name').val('');
        $('#linkman').val('');
        $('#contact_way').val('');
        $('#company_id').val('');

    });

    $(document).on('click', '.company_line', function() {

        var customer_name = $(this).attr('val1');
        var linkman = $(this).attr('val2');
        var contact_way = $(this).attr('val3');
        var company_id = $(this).attr('val0');
        $('#customer_name').val(customer_name);
        $('#linkman').val(linkman);
        $('#contact_way').val(contact_way);
        $('#company_id').val(company_id);

        E.popup.close();

    });

    $('#card_discount').blur(function() {
        $("#card_total_amount").val('');//折后价清空
        var face_value = $('#face_value_id').find('option:selected').text();
        var strs = new Array();
        strs = face_value.split("—");
        face_value=strs[0];
        var card_num = $('#card_num').val();
        var card_discount = $('#card_discount').val();
        var error_msg = '';
        var item_id = '';

        if (face_value == '请选择') {
            error_msg += '请选择面值<br/>';
            item_id = 'face_value_id';
        }

        if ( card_num == '' ) {
            error_msg += '请填写数量<br/>';
            if (item_id == '')
                item_id = 'card_num';
        } else if (!E.isDigital(card_num)) {
            error_msg += '数量必须为整数<br />';
            if (item_id == '')
                item_id = 'card_num';
        }

        if ( card_discount == '' ){
            error_msg += '请填写折扣率<br/>';
            if (item_id == '')
                item_id = 'card_discount';
        } else if ( card_discount < 0 || card_discount > 1) {
            error_msg += '折扣率必须大于等于0小于等于1<br/>';
            if (item_id == '')
                item_id = 'card_discount';
        }

        if (error_msg != '') {
            E.alert(error_msg, 1, function() {
                $('#' + item_id).focus();
            });
        } else {
            var total_amount = face_value * card_num * card_discount;
            //将参考金额写进输入框里，并修改输入框的属性 add by loreta.cheng 2015-06-15 start
            $("#card_total_amount").val(Math.round(total_amount));
            document.getElementById("card_total_amount").disabled = true;
            //将参考金额写进输入框里，并修改输入框的属性 add by loreta.cheng 2015-06-15 end
            //$('#reference').html("参考金额："+parseFloat(total_amount).toFixed(2));
        }

    });

});

$('#apply_date').datepicker();

var cardApplyList = {

    index: 1,

    card_detail: {},

    //申领单检查数据
    save: function() {

        var apply_base = E.getFormValues('itemForm');

        var error_msg = '';
        //回款方式
        var zt='';
        $('input:checkbox:checked').each(function() {
               zt+=$(this).val()+',';
        });
        zt=zt.substring(0,zt.lastIndexOf(','));
        apply_base.backPay=zt;

        if (!zt)
            error_msg += '请选择回款方式<br/>';

        if (apply_base.get_card_type == '')
            error_msg += '请选择领卡方式<br/>';

        if (apply_base.get_card_type == 1) {
            if (E.isEmpty(apply_base.customer_name))
                error_msg += '客户名称不能为空<br/>';
        } else {
            if (E.isEmpty(apply_base.company_id))
                error_msg += '请选择大客户<br/>';
        }

        if (E.isEmpty(apply_base.apply_date))
            error_msg += '申领日期不能为空<br/>';

        if (apply_base.card_total_amount=='')
            error_msg += '请输入现金卡总金额<br/>';

        var count = 0;
        $.each(cardApplyList.card_detail, function() {
            ++count;
        });
        if (count == 0) {
            error_msg += '请录入现金卡信息';
        }

        if (error_msg != '') {
            E.alert(error_msg);
        } else {

            E.confirm('您确认保存申领单吗？', function() {

                E.loadding.open('正在保存，请稍候...');

                E.ajax_post({
                    url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=100&act=edit',
                    data: {
                        apply_base: apply_base,
                        card_detail: cardApplyList.card_detail
                    },
                    call: function( o ) {
                        E.loadding.close();

                        if (o.code == 200) {

                            E.opener("cardApplyList.load");

                            if (apply_base.apply_id > 0) {
                                E.alert(o.message, 2, 'E.closeWindows');
                            } else {
                                E.alert(o.message, 2);
                                document.getElementById('itemForm').reset();
                                $('#add_tr').html('');
                                cardApplyList.card_detail = {};
                                cardApplyList.index = 1;
                            }
                        } else {
                            E.alert(o.message);
                        }

                    }
                });

            });

        }

    },


    del: function( index ) {

        $('#card_line_' + index).remove();
        delete this.card_detail[index];

    },

    //保存检查
    check: function() {

        var dt = E.getFormValues('addForm');

        var error_msg = '';
        if ( dt.face_value_id == '' ) {
            error_msg += '请选择面值<br/>';
        }
        if ( dt.card_type_id == '' ) {
            error_msg += '请选择类型<br/>';
        }
        if ( dt.card_spec_id == '' ) {
            error_msg += '请选择规格<br/>';
        }
        if ( dt.card_num == '' ) {
            error_msg += '请填写数量<br/>';

        } else if (!E.isDigital(dt.card_num)) {
            error_msg += "数量必须为整数<br />";
        }

        if ( dt.card_discount == '' ){
            error_msg += '请填写折扣率<br/>';
        } else if ( dt.card_discount < 0 || dt.card_discount > 1) {
            error_msg += '折扣率必须大于等于0小于等于1<br/>';
        }

        if ( dt.card_total_amount == '' ){
            error_msg += '请填写折后价<br/>';

        } else if(!E.isMoney( dt.card_total_amount)) {
            error_msg += "折后价必须为金额<br />";
        }


        if (error_msg != '') {
            E.alert(error_msg);
            return;
        }

        dt.face_value = $('#face_value_id').find('option:selected').text();
        dt.card_spec = $('#card_spec_id').find('option:selected').text();
        dt.card_type = $('#card_type_id').find('option:selected').text();

        var html = '';

        html += '<tr id="card_line_' + this.index + '">';
        html += '<td style="text-align:center;">';
        html += '<a href="javascript:void(0);" onclick="cardApplyList.del(' + this.index+ ');">删除</a>';
        html += '</td>';
        html += '<td>' + dt.face_value + '</td>';
        html += '<td>' + dt.card_spec + '</td>';
        html += '<td>' + dt.card_num + '</td>';
        html += '<td>' + dt.card_discount + '</td>';
        html += '<td>' + dt.card_total_amount + '</td>';
        html += '<td>' + dt.card_type + '</td>';
        html += '<td>' + dt.card_remark + '</td>';
        html += '</tr>';

        $('#add_tr').append(html);

        this.card_detail[this.index] = dt;
        document.getElementById('addForm').reset();
        this.index++;

    },


    /*
     *查询大客户信息
     */
    search: function(page) {
        var company_name="";
        if($("#company_name").val()!=undefined){
            company_name=$("#company_name").val();
        }
        E.loadding.open('正在查询，请稍候...');

        E.ajax_post({
            url:  "/invokingAjax-backend/company/company.ajax?operFlg=1&properties=2&company_name="+company_name,
            data: {
                sortname: 'company_id',
                sortorder: 'ASC',
                rp: 10,
                page: page,
                list_type: 'general',
                show_type: 'cardApplyList',
                way_type:'search'
            },
            call: function( o ) {

                E.loadding.close();
                if (o.data) {

                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="company_name" name="company_name" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="cardApplyList.search(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    $.each(o.data, function(k, v) {

                        if (v.mobile) {
                            var contact_way = v.mobile;
                        } else {
                            var contact_way = v.phone;
                        }

                        html_str += '<tr>';
                        html_str += '<td class="tac"><a href="javascript:void(0)" class="company_line" val0='+v.company_id+' val1='+v.company_name+' val2='+ v.connect_name+' val3='+ contact_way +'>选择</a></td>';
                        html_str += '<td class="tac" style="text-align:left">' + v.company_name + '</td>';
                        html_str += '<td class="tac">' + v.connect_name + '</td>';
                        html_str += '<td class="tac">' + contact_way + '</td>';
                        html_str += '<td class="tac">' + v.departmentName + '</td>';
                        html_str += '<td class="tac">' + v.userName + '</td>';
                        html_str += '</tr>';

                    });
                    html_str += '</tbody>';
                    html_str += '</table>';


                    if (o.paging) {
                        html_str += o.paging;
                    }

                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });

                }else{
                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="company_name" name="company_name" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="cardApplyList.search(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    html_str += '</tbody>';
                    html_str += '</table>';
                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });
                }

            }
        });

    }

};

<!--{if isset($card_detail_array)}-->
<!--{foreach from=$card_detail_array key=k item=detail}-->
cardApplyList.card_detail[cardApplyList.index] = {
    face_value_id: '<!--{$detail.face_value_id}-->',
    face_value: '<!--{$detail.face_value}-->',
    card_spec_id: '<!--{$detail.card_spec_id}-->',
    card_spec: '<!--{$detail.card_spec}-->',
    card_type_id: '<!--{$detail.card_type_id}-->',
    card_type: '<!--{$detail.card_type}-->',
    card_num: '<!--{$detail.card_num}-->',
    card_discount: '<!--{$detail.card_discount}-->',
    card_total_amount: '<!--{$detail.card_total_amount}-->',
    card_remark: '<!--{$detail.remark}-->'
};
cardApplyList.index++;
<!--{/foreach}-->

$('#get_card_type').val(<!--{$card_apply_array.get_card_type}-->);
<!--{if $card_apply_array.get_card_type == 1}-->
$('#setMapBtn').hide();
$('#customer_name').attr('readonly', false);
<!--{/if}-->

<!--{/if}-->

</script>

</body>

</html>
